package com.example.springboot.controller;

import cn.hutool.poi.excel.ExcelUtil;
import cn.hutool.poi.excel.ExcelWriter;
import com.example.springboot.entity.*;
import com.example.springboot.mapper.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.net.URLEncoder;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/lists")
public class ListsController {
    @Autowired
    private OrderMapper orderMapper;

    @Autowired
    private ApplicationMapper applicationMapper;

    @Autowired
    private AbandonMapper abandonMapper;

    @Autowired
    private ApprovalMapper approvalMapper;

    @Autowired
    private UserMapper userMapper;

   // 按照用户姓名分页查询
    @GetMapping("/page")
    public Map<String,Object> findList(@RequestParam Integer pageNum,
                                         @RequestParam Integer pageSize,
                                         @RequestParam String userName){
        Integer userId = userMapper.selectId(userName);
        List<Lists> data = new LinkedList<>();
        List<Order> orders = orderMapper.findByUser(userId);
        for(Order order : orders){
            Lists list = new Lists();
            list.setType("采购申请单");
            list.setNumber("P"+order.getId().toString());
            list.setApplyTime(order.getCreateTime());
            list.setIsApprove(order.getIsApprove());
            if(order.getApprovalId()!=null){
                Approval app = approvalMapper.findById(order.getApprovalId());
                list.setIsAgree(app.getIsAgree());
                list.setNote(app.getNote());
                list.setApproverName(userMapper.selectName(app.getApproverId()));
                list.setApproveTime(app.getCreateTime());
            }
            data.add(list);
        }
        List<Application> applications = applicationMapper.findByUser(userId);
        for(Application application : applications){
            Lists list = new Lists();
            list.setType("领用申请单");
            list.setNumber("A"+application.getId().toString());
            list.setApplyTime(application.getCreateTime());
            list.setIsApprove(application.getIsApprove());
            if(application.getApprovalId()!=null){
                Approval app = approvalMapper.findById(application.getApprovalId());
                list.setIsAgree(app.getIsAgree());
                list.setNote(app.getNote());
                list.setApproverName(userMapper.selectName(app.getApproverId()));
                list.setApproveTime(app.getCreateTime());
            }
            data.add(list);
        }
        List<Abandon> abandons = abandonMapper.findByUser(userId);
        for(Abandon abandon : abandons){
            Lists list = new Lists();
            list.setType("报废申请单");
            list.setNumber("T"+abandon.getId().toString());
            list.setApplyTime(abandon.getCreateTime());
            list.setIsApprove(abandon.getIsApprove());
            if(abandon.getApprovalId()!=null){
                Approval app = approvalMapper.findById(abandon.getApprovalId());
                list.setIsAgree(app.getIsAgree());
                list.setNote(app.getNote());
                list.setApproverName(userMapper.selectName(app.getApproverId()));
                list.setApproveTime(app.getCreateTime());
            }
            data.add(list);
        }
        Integer total = data.size();
        //分页
        pageNum = (pageNum - 1) * pageSize;
        List<Lists> dataByPage = data.subList(pageNum, Math.min(pageNum + pageSize, total));
        Map<String,Object> res = new HashMap<>();
        res.put("data",dataByPage);
        res.put("total",total);
        return res;
    }


//按用户姓名查找所有申请单
    @GetMapping
    public List<Lists> findAll(@RequestParam String userName){

        Integer userId = userMapper.selectId(userName);
        List<Lists> test = new LinkedList<>();
        List<Order> orders = orderMapper.findByUser(userId);
        for(Order order : orders){
            Lists list = new Lists();
            list.setType("采购申请单");
            list.setNumber("P"+order.getId().toString());
            list.setApplyTime(order.getCreateTime());
            list.setIsApprove(order.getIsApprove());
            Approval app = approvalMapper.findById(order.getApprovalId());
            list.setIsAgree(app.getIsAgree());
            list.setNote(app.getNote());
            list.setApproverName(userMapper.selectName(app.getApproverId()));
            list.setApproveTime(app.getCreateTime());
            test.add(list);
        }
        List<Application> applications = applicationMapper.findByUser(userId);
        for(Application application : applications){
            Lists list = new Lists();
            list.setType("领用申请单");
            list.setNumber("A"+application.getId().toString());
            list.setApplyTime(application.getCreateTime());
            list.setIsApprove(application.getIsApprove());
            Approval app = approvalMapper.findById(application.getApprovalId());
            list.setIsAgree(app.getIsAgree());
            list.setNote(app.getNote());
            list.setApproverName(userMapper.selectName(app.getApproverId()));
            list.setApproveTime(app.getCreateTime());
            test.add(list);
        }
        List<Abandon> abandons = abandonMapper.findByUser(userId);
        for(Abandon abandon : abandons){
            Lists list = new Lists();
            list.setType("报废申请单");
            list.setNumber("T"+abandon.getId().toString());
            list.setApplyTime(abandon.getCreateTime());
            list.setIsApprove(abandon.getIsApprove());
            Approval app = approvalMapper.findById(abandon.getApprovalId());
            list.setIsAgree(app.getIsAgree());
            list.setNote(app.getNote());
            list.setApproverName(userMapper.selectName(app.getApproverId()));
            list.setApproveTime(app.getCreateTime());
            test.add(list);
        }


        return test;//查询所有数据
    }


}
